<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回对象的类名</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.get-class-vars.html">get_class_vars</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.get-declared-classes.html">get_declared_classes</a></div>
 <div class="up"><a href="ref.classobj.html">类/对象 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.get-class" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">get_class</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">get_class</span> &mdash; <span class="dc-title">返回对象的类名</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.get-class-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>get_class</strong></span>
    ([ <span class="methodparam"><span class="type">object</span> <code class="parameter">$obj</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   返回对象实例 <em><code class="parameter">obj</code></em>
   所属类的名字。如果 <em><code class="parameter">obj</code></em>
   不是一个对象则返回 <strong><code>FALSE</code></strong>。
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    在 PHP 扩展库中定义的类返回其原始定义的名字。在 PHP 4 中
    <span class="function"><strong>get_class()</strong></span> 返回用户定义的类名的小写形式，但是在
    PHP 5 中将返回类名定义时的名字，如同扩展库中的类名一样。
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    自 PHP 5 起，如果在对象的方法中调用则
    <em><code class="parameter">obj</code></em> 为可选项。
   </p>
  </p></blockquote>
  <p class="para">
   <div class="example" id="example-4951">
    <p><strong>Example #1 使用 <span class="function"><strong>get_class()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;implements&nbsp;some&nbsp;logic<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"My&nbsp;name&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">)&nbsp;,&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;create&nbsp;an&nbsp;object<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;external&nbsp;call<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Its&nbsp;name&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">)&nbsp;,&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;internal&nbsp;call<br /></span><span style="color: #0000BB">$bar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Its name is foo
My name is foo
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   参见 <span class="function"><a href="function.get-parent-class.html" class="function">get_parent_class()</a></span>、
   <span class="function"><a href="function.gettype.html" class="function">gettype()</a></span> 和
   <span class="function"><a href="function.is-subclass-of.html" class="function">is_subclass_of()</a></span>。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.get-class-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">object</code></em></dt>

     <dd>

      <p class="para">
       The tested object. This parameter may be omitted when inside a class.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.get-class-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the name of the class of which <em><code class="parameter">object</code></em> is an
   instance. Returns <strong><code>FALSE</code></strong> if <em><code class="parameter">object</code></em> is not an 
   object.
  </p>
  <p class="para">
   If <em><code class="parameter">object</code></em> is omitted when inside a class, the
   name of that class is returned.
  </p>
 </div>

 <div class="refsect1 errors" id="refsect1-function.get-class-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   If <span class="function"><strong>get_class()</strong></span> is called with anything other than an
   object, an <strong><code>E_WARNING</code></strong> level error is raised.
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.get-class-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>Since 5.3.0</td>
       <td>
        <strong><code>NULL</code></strong> became the default value for <em><code class="parameter">object</code></em>,
        so passing <strong><code>NULL</code></strong> to <em><code class="parameter">object</code></em> now has the same
        result as not passing any value.
       </td>
      </tr>

      <tr>
       <td>Since 5.0.0</td>
       <td>
        The class name is returned in its original notation.
       </td>
      </tr>

      <tr>
       <td>Since 5.0.0</td>
       <td>
        The <em><code class="parameter">object</code></em> parameter is optional if called
        from the object&#039;s method.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.get-class-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4952">
    <p><strong>Example #2 Using <span class="function"><strong>get_class()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"My&nbsp;name&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">)&nbsp;,&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;create&nbsp;an&nbsp;object<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;external&nbsp;call<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Its&nbsp;name&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">)&nbsp;,&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;internal&nbsp;call<br /></span><span style="color: #0000BB">$bar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Its name is foo
My name is foo
</pre></div>
    </div>
   </div>
   <div class="example" id="example-4953">
    <p><strong>Example #3 Using <span class="function"><strong>get_class()</strong></span> in superclass</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">abstract&nbsp;class&nbsp;</span><span style="color: #0000BB">bar&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">bar&nbsp;</span><span style="color: #007700">{<br />}<br /><br />new&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(3) &quot;foo&quot;
string(3) &quot;bar&quot;
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.get-class-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.get-called-class.html" class="function" rel="rdfs-seeAlso">get_called_class()</a> - 后期静态绑定（&quot;Late Static Binding&quot;）类的名称</span></li>
    <li class="member"><span class="function"><a href="function.get-parent-class.html" class="function" rel="rdfs-seeAlso">get_parent_class()</a> - 返回对象或类的父类名</span></li>
    <li class="member"><span class="function"><a href="function.gettype.html" class="function" rel="rdfs-seeAlso">gettype()</a> - 获取变量的类型</span></li>
    <li class="member"><span class="function"><a href="function.is-subclass-of.html" class="function" rel="rdfs-seeAlso">is_subclass_of()</a> - 如果此对象是该类的子类，则返回 TRUE</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.get-class-vars.html">get_class_vars</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.get-declared-classes.html">get_declared_classes</a></div>
 <div class="up"><a href="ref.classobj.html">类/对象 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
